home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / telecomm / uusrc.arc / UUENCODE.C < prev   
Encoding:
C/C++ Source or Header  |  1987-02-16  |  4.5 KB  |  173 lines

  1. /*
  2.  *
  3.  * Uuencode -- encode a file so that it's printable ascii, short lines
  4.  *
  5.  * Slightly modified from a version posted to net.sources a while back,
  6.  * and suitable for compilation on the IBM PC
  7.  *
  8.  * modified for Lattice C on the ST - 11.05.85 by MSD
  9.  * modified for ALCYON on the ST -    10-24-86 by RDR
  10.  * modified a little more for MWC...  02/09/87 by JPHD
  11.  * (An optional first argument of the form: -nnumber (e.g. -500), will
  12.  * produce a serie of files that long, linked by the include statement,
  13.  * such files are automatically uudecoded by the companion program.)
  14.  * (A lot more to do about I/O speed, avoiding completely the stdio.h...)
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <ctype.h>
  21.  
  22. #define USAGE "Usage: uuencode [-n] inputfile\n"
  23.  
  24. /* ENC is the basic 1 character encoding function to make a char printing */
  25. #define ENC(c) (((c) & 077) + ' ')
  26.  
  27. extern FILE  *freopen(), *fopen();
  28. FILE *fp;
  29. char ofname[80];
  30. extern char *rindex();
  31. int part = 'a';
  32. int split = 0; fileln = 32000;
  33.  
  34. main(argc, argv)
  35. int argc; char *argv[];
  36. {
  37.         if (argc < 2) {
  38.                 fprintf(stderr, USAGE);
  39.                 exit(2);
  40.                 }
  41.     if (argv[1][0] == '-') {
  42.         fileln = -atoi(argv[1]);
  43.         if (fileln <= 0) {
  44.             fprintf(stderr, "Wrong file length arg.\n");
  45.             exit();
  46.         }
  47.         split = 1;
  48.         argv++;
  49.     }
  50.         makename(argv[1]);
  51.         if ((fp=fopen(argv[1], "rb"))==NULL) {  /* binary input !!! */
  52.                 fprintf(stderr,"Cannot open %s\n",argv[1]);
  53.                 exit(1);
  54.         }
  55.         if(freopen(ofname, "w", stdout)!=stdout) {
  56.                 fprintf(stderr,"Cannot reassign stdout\n");
  57.                 exit(1);
  58.                 }
  59.         maketable();
  60.         printf("begin %o %s\n", 0644, argv[1]);
  61.         encode();
  62.         printf("end\n");
  63.         exit(0);
  64. }
  65.  
  66. /* create ASCII table so a mailer can screw it up and the decode
  67.  * program can restore the error.
  68.  */
  69. maketable()
  70. {
  71.         register int i, j;
  72.  
  73.         puts("table");
  74.         for(i = ' ', j = 0; i < '`' ; j++) {
  75.                 if (j == 32)
  76.             putchar('\n');
  77.         fputc(i++,stdout);
  78.         }
  79.         putchar('\n');
  80. }
  81.  
  82. /* I include this in all of my programs to take the guess work out of
  83.  * filenames.
  84.  */
  85. makename(name)
  86. char name[];
  87. {
  88.         register char *ptr;
  89.  
  90.         strcpy(ofname, name);
  91.         /* I think index is neat; just look for a character, and
  92.          * bomb it.  Voila, you have a substring, no mess.
  93.          */
  94.         if(ptr = rindex(ofname,'.')) {
  95.                 *ptr = '\0';
  96.                 *--ptr = part;
  97.         }
  98.         else /* i.e. make the last character in the first name = part */
  99.                 ofname[strlen(ofname)-1] = part;
  100.         strcat(ofname,".uue");
  101.         return;
  102. }
  103.  
  104. /*
  105.  * copy from stdin to stdout, encoding as you go along.
  106.  */
  107. encode()
  108. {
  109.         char buf[80];
  110.         char file[80];
  111.         register int i, n;
  112.         register int lines;
  113.         lines = 6;
  114.  
  115.         strcpy(file,ofname);
  116.         for (;;) {
  117.                 n = fr(buf, 45);
  118.                 putchar(ENC(n));
  119.                 for (i = 0; i < n; i += 3)
  120.                       outdec(&buf[i]);
  121.                 putchar(part);
  122.                 putchar('\n');
  123.                 ++lines;
  124.                 if (split && (lines > fileln)) {
  125.                         ++part;
  126.                         makename(file);
  127.                         printf("include %s\n",ofname);
  128.                         if(freopen(ofname, "w", stdout)!=stdout) {
  129.                                 fprintf(stderr,"Cannot reassign stdout\n");
  130.                                 exit(1);
  131.                         }
  132.                         maketable();
  133.                         printf("begin part %c\n",part);
  134.                         lines = 6;
  135.                 }
  136.                 if (n <= 0)
  137.                         break;
  138.         }
  139. }
  140.  
  141. /*
  142.  * output one group of 3 bytes, pointed at by p, on file f.
  143.  */
  144. outdec(p)
  145. register char *p;
  146. {
  147.         register int c1, c2, c3, c4;
  148.  
  149.         c1 = *p >> 2;
  150.         c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  151.         c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  152.         c4 = p[2] & 077;
  153.         putchar(ENC(c1));
  154.         putchar(ENC(c2));
  155.         putchar(ENC(c3));
  156.         putchar(ENC(c4));
  157. }
  158.  
  159. /* fr: like read but stdio */
  160. int fr(buf, cnt)
  161. register char *buf;
  162. register int cnt;
  163. {
  164.         register int c, i;
  165.         for (i = 0; i < cnt; i++) {
  166.                 c = fgetc(fp);
  167.                 if (feof(fp))
  168.                         return(i);
  169.                 buf[i] = c;
  170.         }
  171.         return (cnt);
  172. }
  173.